package net.zhangbaolin.systemadmin.controller.user;

import net.zhangbaolin.systemadmin.common.ResponseJson;
import net.zhangbaolin.systemadmin.entity.Login;
import net.zhangbaolin.systemadmin.entity.User;
import net.zhangbaolin.systemadmin.service.impl.LoginServiceImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class LoginController {

    @Resource
    LoginServiceImpl loginService;

    // 登录
    @PostMapping("/login")
    public ResponseJson login(@RequestBody User user) {
        return loginService.login(user);

    }

    public ResponseJson logout() {
        return loginService.logout();
    }

    //获取验证码
    @GetMapping("/getCaptcha")
    public String getCaptcha() {
        return null;
    }

    @PostMapping("/info")
    public ResponseJson info(){
        Authentication authenticate =  SecurityContextHolder.getContext().getAuthentication();
        Login login = (Login) authenticate.getPrincipal();
        User user = login.getUser();
        user.setPassword(null);
        return ResponseJson.ok(user);
    }
}

